package com.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Java8Map {
    private static void getOrDefault() {
        Map<String, String> map1 = new HashMap<>();
        // get一个不存在的key
        String v1 = map1.getOrDefault("k1", "5566");
        System.out.println("v1=" + v1 + ", v1==5566:" + "5566".equals(v1));
        // get一个存在但为null的key
        map1.put("k2", null);
        String v2 = map1.getOrDefault("k2", "5566");
        System.out.println("v2=" + v2 + ", v2==5566:" + "5566".equals(v2));
    }

    private static void replaceAll() {
        Map<String, Integer> map1 = new HashMap<>();
        // 添加几个数据
        map1.put("a", 2);
        map1.put("b", 3);
        map1.put("c", 1);

        // 将所有大于1的value替换为0
        map1.replaceAll((k, v) -> v > 1 ? 0 : v);
        map1.forEach((k, v) -> System.out.println(k + ": " + v + ", v > 1?" + (v > 1)));
    }

    private static void putIfAbsent() {
        Map<String, String> map1 = new HashMap<>();
        // 添加几个数据
        map1.put("a", "a1");
        map1.put("b", "b1");

        // 需要添加的kv数据
        List<String> ks = new ArrayList<String>() {{add("a");add("c");}};
        List<String> vs = new ArrayList<String>() {{add("a2");add("c1");}};

        // 将不存在的kv数据加进map
        for (int i = 0; i < ks.size(); i++) {
            // 这一步的返回值是旧value，和put返回值一样
            map1.putIfAbsent(ks.get(i), vs.get(i));
        }

        map1.forEach((k, v) -> System.out.println(k + ": " + v));
    }

    private static void computeIfAbsent() {
        Map<String, List<String>> map1 = new HashMap<>();
        // 添加几个数据
        map1.put("a", new ArrayList<>());
        map1.put("b", null);

        // 如果不存在key就会返回新值
        List<String> cList = map1.computeIfAbsent("c", k -> new ArrayList<>());
        System.out.println(cList != null);  // true

        // 如果存在key，但value为null也会返回新值
        List<String> bList = map1.computeIfAbsent("b", k -> new ArrayList<>());
        System.out.println(bList != null);  // true
    }

    private static void computeIfPresent() {
        Map<String, Integer> map1 = new HashMap<>();
        // 添加几个数据
        map1.put("a", 0);
        map1.put("b", null);

        // 只有当key对应value!=null时才会执行计算并返回计算结果
        Integer v1 = map1.computeIfPresent("a", (k, v) -> v + 1);
        System.out.println(v1 == 1);  // true

        // 当value为空时会返回null，即存在key但value==null 或者 不存在key
        Integer v2 = map1.computeIfPresent("b", (k, v) -> v + 1);
        System.out.println(v2 == null);  // true
        Integer v3 = map1.computeIfPresent("c", (k, v) -> v + 1);
        System.out.println(v3 == null);  // ture
    }

    private static void merge() {
        Map<String, Integer> map1 = new HashMap<>();
        // 添加几个数据
        map1.put("a", 0);
        map1.put("b", null);

        // 当key对应旧value!=null时会将给的value与旧value计算，并替换原value后返回
        Integer v1 = map1.merge("a", 1, (ov, nv) -> ov + nv);
        System.out.println(v1 == 1); // true

        // 如果key对应旧value==null，会将给定value替换旧value，并返回给的value
        Integer v2 = map1.merge("b", 2, Integer::sum);
        System.out.println(v2 == 2);  // true
        Integer v3 = map1.merge("c", 3, Integer::sum);
        System.out.println(v3 == 3);  // true

        map1.forEach((k, v) -> System.out.println(k + ": " + v));
    }

    public static void main(String[] args) {
        merge();
    }
}
